package TankGame5;

import java.io.*;
import java.util.Vector;

public class Recorder {
    //定义变量，记录我方坦克击毁敌方坦克数量
    private static int allEnemyTankNum;
    //定义io对象 准备写数据到文件中
    private static BufferedWriter bufferedWriter = null;
    private static BufferedReader bufferedReader = null;
    //定义Vector,指向MyPanel对象的敌人坦克Vector
    private static Vector<Enemy> enemies = null;
    //定义一个Node的Vector,用于保存敌人的信息
    private static Vector<Node> nodes = new Vector<>();
    private static String recorderfile = "D:\\myRecorder.txt";

    public static String getRecorderfile() {
        return recorderfile;
    }

    public static void setEnemies(Vector<Enemy> enemies) {
        Recorder.enemies = enemies;
    }
    //增加一个方法，用于读取recordFile, 恢复相关信息
    //该方法，在继续上局的时候调用即可
    public static Vector<Node> getNodesAndEnemyTankRec() {

        try {

            bufferedReader = new BufferedReader(new FileReader(recorderfile));
            allEnemyTankNum = Integer.parseInt(bufferedReader.readLine());
            //循环读取文件，生成nodes 集合
            String line = "";//255 40 0
            while ((line = bufferedReader.readLine())!=null) {
                String[] xyd = line.split(" ");
                Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]),
                        Integer.parseInt(xyd[2]));
                nodes.add(node);//放入nodes Vector
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return nodes;
    }


    //增加一个方法，当游戏退出时，我们将allEnemyTankNum 保存到 recordFile
    //对keepRecord 进行升级, 保存敌人坦克的坐标和方向

    public static void keepRecord() {
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(recorderfile));
            bufferedWriter.write(allEnemyTankNum + "\r\n");
            //遍历敌人坦克的Vector ,然后根据情况保存即可.
            //OOP, 定义一个属性 ，然后通过setXxx得到 敌人坦克的Vector
            for (int i = 0; i < enemies.size(); i++) {
                //取出敌人坦克
                Enemy enemyTank = enemies.get(i);

                    //保存该enemyTank信息
                    String record = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect();
                    //写入到文件
                    bufferedWriter.write(record + "\r\n");

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    //当我方坦克击毁一辆敌方坦克，就应当allEnemyTankNum++
    public static void addAllEnemyTankNum(){
        Recorder.allEnemyTankNum++;
    }
}
